﻿

Ext.define('app.prm.product.Phase', {
    extend: 'Ext.grid.Panel',
    border: false,
    viewConfig: {
        enableTextSelection: true //可以复制单元格文字
    },
    title:'项目阶段',
    forceFit: false,
    productId: 0,
    enableLocking: true,
    columns: [],
    columnLines: true,
    initComponent: function () {
        var me = this;

      

        me.tbar = [
            {
                text: '已完成',
                width: 100,
                xtype: 'tbtext',
                cls: 'phaseFinsh'
            },
            {
                text: '未完成',
                width: 100,
                xtype: 'tbtext',
                cls: 'phaseUnFinsh'
            },
            {
                text: 'N/A',
                width: 100,
                xtype: 'tbtext',
                cls: 'phaseUnKown'
            }
            //,
            //{
            //    text: '刷新',
            //    glyph: 0xf021,
            //    handler: function () {
            //        me.loadData();
            //    }
            //}
        ];
        me.callParent();
    },
    listeners: {
        afterrender: function () {
            this.loadData();
        },
        celldblclick: 'onCellClick'
    },
    loadData: function () {
        this.loadmask = new Ext.LoadMask(this, { msg: "loading" });
        this.loadmask.show();
        var main = this;
        main.requestDataNum = 0;
        App.AjaxGet('/api/services/app/phaseInfo/GetPhaseInfo', function (data) {
            //loadPhase
            main.phaseItems = data.result;
            main.dataReady();
        });
        App.AjaxGet('/api/services/app/project/GetProjectPhaseReport?productId=' + this.productId, function (data) {
            //loadProduct
            main.projectPhaseItems = data.result;
            main.dataReady();
        });
    },
    dataReady: function () {

        this.requestDataNum = this.requestDataNum + 1;
        if (this.requestDataNum > 1) {
            this.loadmask.hide();
            this.project_phase_detial();
        }

    },
    project_phase_detial: function () {
        var main = this;
        
            main.createProjectPhaseColumnFields(true);
            main.loadProjectPhaseStore();

    },
    createProjectPhaseColumnFields: function (isPhaseDetial) {
        var main = this;
        var columns = [];
        var fields = [];
        columns.push({ header: '客户', dataIndex: 'customer', locked: true });
        columns.push({ header: '项目名称', dataIndex: 'projectName', locked: true });
        columns.push({
            header: '项目编号', dataIndex: 'projectNo', locked: true, renderer:
                function (val) {
                    return '<span style="color:blue">' + val + '</span>'
                }
        });
        //columns.push({
        //    header: '产品号', dataIndex: 'productNo', locked: true
        //});
        //customer
        fields.push('customer');
        fields.push('projectId');
        fields.push('productId');
        fields.push('projectName');
        fields.push('projectNo');
        fields.push('productNo');

        var phaseItems = main.phaseItems;
        for (var i = 0; i < phaseItems.length; i++) {
            var record = phaseItems[i];

            var obj = {
                header: record.name, dataIndex: record.name, renderer: main.renderPhase
            };
            fields.push(record.name);
            fields.push(record.name + "IsFinsh");
            if (isPhaseDetial && record.phaseItems.length > 0) {
                var firstColumn = { header: record.name, columns: [] };
                firstColumn.columns.push(obj);
                for (var j = 0; j < record.phaseItems.length; j++) {
                    var child = record.phaseItems[j];
                    firstColumn.columns.push({
                        header: child.name, dataIndex: child.name, renderer: main.renderPhase
                    });
                    fields.push(child.name);
                    fields.push(child.name + "IsFinsh");
                }
                columns.push(firstColumn);
            }
            else {
                columns.push(obj);
            }
        }
        this.summryColumns = columns;
        this.summryFields = fields;
    },
    loadProjectPhaseStore: function () {
        var summryFields = this.summryFields;
        var store = new Ext.data.JsonStore({
            fields: summryFields
        });

        //ProjectPhase_ProjectPhaseModel.removeFields(ProjectPhase_ProjectPhaseModel.fields);
        //ProjectPhase_ProjectPhaseModel.addFields(summryFields);

        var projectPhaseItems = this.projectPhaseItems;
        for (var i = 0; i < projectPhaseItems.length; i++) {
            var data = projectPhaseItems[i];
            var p = Ext.create("Ext.data.Model", {
            });

            for (var j = 0; j < summryFields.length; j++) {
                if (summryFields[j].indexOf("IsFinsh") > 0) {
                    if (data[summryFields[j]] != undefined) {
                        p.set(summryFields[j], data[summryFields[j]] ? 1 : 0);
                    } else {
                        p.set(summryFields[j], -1);
                    }

                } else {
                    if (data[summryFields[j]]) {
                        p.set(summryFields[j], data[summryFields[j]]);
                    }
                }

            }
            p.commit();
            store.add(p);
        } 
        
        this.reconfigure(store, this.summryColumns);
    },

    renderPhase: function (value, p, _record) {

        if (!value) {
            value = '';
        }
        var isFinish = _record.data[p.column.dataIndex + "IsFinsh"];
        if (isFinish >= 0) {
            p.tdCls = isFinish > 0 ? "phaseFinsh" : "phaseUnFinsh";
            p.tdAttr = 'data-qtip="' + value + '"';
        }
        else {
            p.tdCls = "phaseUnKown";
        }
        return value;
    }
});